import { _decorator, BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, RigidBody2D, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ArrowControl')
export class ArrowControl extends Component {
  @property(Number)
  speed: number = 20
  direction: boolean = false
  rigidbody: RigidBody2D = null

  onLoad() {
    this.rigidbody = this.getComponent(RigidBody2D)
  }
  start() {
    this.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
    const x = this.direction ? this.speed : -this.speed
    this.rigidbody.linearVelocity = new Vec2(x, 0)
    this.rigidbody.angularVelocity = 0.3
    this.schedule(() => {
      this.node?.destroy()
    }, 5, 1)
  }

  update(deltaTime: number) {

  }
  onBeginContact(self: Collider2D, other: Collider2D, contact: IPhysics2DContact){
    if (other.tag===0){
      this.schedule(() => {
        this.node?.destroy()
      },0)
    }
  }
}


